package com.woniu.elderly.operation.controller;

import com.woniu.common.entity.Opecomment;
import com.woniu.common.entity.ResponseResult;
import com.woniu.elderly.operation.service.OpecommentService;
import com.woniu.elderly.operation.service.OpelikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RefreshScope
@Slf4j
@RestController
@RequestMapping("/like")
public class OpelikeController {
    @Autowired
    private OpelikeService opelikeService;

    @PostMapping("/likeArticle/{nid}/{eid}")
    public ResponseResult<String> likeArticle(@PathVariable Long nid, @PathVariable Long eid) {
        opelikeService.likeArticle(nid, eid);
        return ResponseResult.ok("点赞成功");
    }

    @PostMapping("/unlikeArticle/{nid}/{eid}")
    public ResponseResult<String> unlikeArticle(@PathVariable Long nid, @PathVariable Long eid) {
        opelikeService.unlikeArticle(nid, eid);
        return ResponseResult.ok("已取消点赞");
    }

    @GetMapping("/{nid}/{eid}")
    public ResponseResult<Boolean> checkLike(@PathVariable Long nid, @PathVariable Long eid) {
        boolean liked = opelikeService.hasLiked(nid, eid);
        return ResponseResult.ok(liked);
    }

    // 获取文章的点赞数
    @GetMapping("/count/{nid}")
    public ResponseResult<Long> getLikesCount(@PathVariable Long nid) {
        long likesCount = opelikeService.getLikesCount(nid);
        return ResponseResult.ok(likesCount);
    }

}
